package org.apm.data.copy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apm.data.Config;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CopyConfig extends Config {

    // 源集群相关配置
    static final String SRC_CLUSTER_NAME;
    static final String SRC_CLUSTER_MASTER_IP;
    static final Map<String, Integer> SRC_HOSTS = new HashMap<>();
    // 目的集群相关配置
    static final String DST_CLUSTER_NAME;
    static final String DST_CLUSTER_MASTER_IP;
    static final Map<String, Integer> DST_HOSTS = new HashMap<>();

    static final Set<String> COPY_MULTI_TYPE;
    private static final Logger LOGGER = LogManager.getLogger(CopyConfig.class);
    private static final String COPY_PROPERTIES = "copy.properties";

    static {
        LOGGER.info("Reading configuration:" + COPY_PROPERTIES);

        Properties props = loadProperties(COPY_PROPERTIES);

        //
        SRC_CLUSTER_NAME = props.getProperty("SRC_es_cluster_name", "Apm");
        SRC_CLUSTER_MASTER_IP = props.getProperty("SRC_es_cluster_masterIp", "harmonycloud");
        String SRC_HOST_STR = props.getProperty("SRC_es_host_address", "[localhost]");


        // es
        DST_CLUSTER_NAME = props.getProperty("DST_es_cluster_name", "Apm");
        DST_CLUSTER_MASTER_IP = props.getProperty("DST_es_cluster_masterIp", "harmonycloud");
        String DST_HOST_STR = props.getProperty("DST_es_host_address", "[localhost]");

        COPY_MULTI_TYPE = JSON.parseObject(props.getProperty("copy_multi_type", "[]")
                , new TypeReference<Set<String>>() {
                });

        try {
            getHosts(SRC_HOST_STR, SRC_HOSTS);
            getHosts(DST_HOST_STR, DST_HOSTS);
        } catch (Exception exception) {
            LOGGER.error(exception.getMessage(), exception);
        }

        LOGGER.info("elasticsearch-client:"
                + " Src { clusterName: " + SRC_CLUSTER_NAME + " ,host: " + SRC_HOSTS
                + " ,masterIp: " + SRC_CLUSTER_MASTER_IP + " }"
                + " Dst { clusterName: " + DST_CLUSTER_NAME + ", dst_hosts: " + DST_HOSTS
                + " ,masterIp: " + DST_CLUSTER_MASTER_IP + " }");
    }
}
